<template>
  <div>
    <el-form :inline="true" ref="deviceForm" :model="queryParams">
        <section class="justify-align">
          <div>
            <el-form-item>
              <el-button v-if="checkAuthority('/equilable/add')" type="primary" @click="addDeviceTag">新增设备标签</el-button>
            </el-form-item>
          </div>
          <div>
            <el-form-item>
              <el-select v-model="queryParams.labelTypeId" clearable placeholder="请选择标签类型">
                <el-option
                  v-for="item in eqptTagTypeList"
                  :key="item.labelTypeId"
                  :label="item.labelTypeName"
                  :value="item.labelTypeId">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-input clearable placeholder="请输入标签名称" v-model="queryParams.labelName"></el-input>
            </el-form-item>
            <el-form-item class="searchBtn">
              <el-button v-if="checkAuthority('/select')" type="primary" @click="searchQuery">查询</el-button>
            </el-form-item>
          </div>
        </section>
      </el-form>
      <el-table :data="tableData" style="width: 100%" row-key="userNo">
        <el-table-column type="index" label="序号" width="100"></el-table-column>
        <el-table-column prop="labelNumber" label="设备标签编号"></el-table-column>
        <el-table-column prop="labelName" label="标签名称"></el-table-column>
        <el-table-column prop="labelTypeName" label="标签类型"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button v-if="checkAuthority('/equilable/edtior')" type="text" size="small" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button v-if="checkAuthority('/equilable/delete')" type="text" size="small" @click="handleRemove(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        v-if="tableData.length>0"
        style="text-align: right"
        layout="total, prev, pager, next"
        @current-change="pageHandleChange"
        :current-page="queryParams.current"
        :page-size="queryParams.pageSize"
        :total="totalPageNum"
      ></el-pagination>

    <add-device-tag
      :eqptTagTypeList="eqptTagTypeList"
      :modalObj="modalObj"
      v-if="modalObj.show"
      @ok="ok"
      @close="handleClose"
    ></add-device-tag>
  </div>
</template>

<script>
import { getLabelList, deleteDeviceLabel, getLabelTypeList } from '@/views/children/safety/js/device'
import addDeviceTag from './addDeviceTag.vue'
import { checkAuthority } from '@/utils/btnPermission' // 页面按钮权限
export default {
  name: 'DeviceTag',
  components: {
    addDeviceTag
  },
  data () {
    return {
      tagId: '',
      totalPageNum: 0,
      queryParams: {
        current: 1,
        pageSize: 10,
        labelName: '',
        labelTypeId: ''
      },
      tableData: [],
      eqptTagTypeList: [],
      modalObj: {
        titile: '',
        show: false,
        row: {}
      }
    }
  },
  mounted () {
    this.getLabelTypeList()
    this.searchQuery()
  },
  methods: {
    checkAuthority,
    // 新增标签类型 按钮
    addDeviceTag () {
      this.modalObj.show = true
      this.modalObj.titile = '新增设备标签'
      this.modalObj.row = {}
    },
    // 搜索 按钮
    searchQuery () {
      this.queryParams.current = 1
      this.getListInit()
    },
    // 编辑
    handleEdit (row) {
      this.modalObj.show = true
      this.modalObj.titile = '编辑设备标签'
      this.modalObj.row = row
    },
    ok () {
      this.modalObj.show = false
      this.searchQuery()
    },
    handleClose () {
      this.modalObj.show = false
    },
    // 删除
    handleRemove (row) {
      this.$confirm('确定删除该标签吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        let result = await deleteDeviceLabel(row.labelId)
        if (result.code === 0) {
          this.getListInit()
          this.$message.success('删除成功！')
        } else {
          this.$message.error(result.msg + '删除失败！')
        }
      })
    },
    async getLabelTypeList () {
      // 设备标签类型下拉数据
      try {
        const res = await getLabelTypeList({
          map: {}
        })
        if (res.code === 0) {
          this.eqptTagTypeList = res.data
        }
      } catch (e) {}
    },
    // 设备标签列表
    async getListInit () {
      try {
        let res = await getLabelList({
          ...this.queryParams
        })
        if (res.code === 0) {
          this.tableData = res.data.records
          this.totalPageNum = res.data.total
        }
      } catch (e) {}
    },
    pageHandleChange (val) {
      this.queryParams.current = val
      this.getListInit()
    }
  }
}
</script>

<style scoped lang="scss">
</style>
